package com.ant.myagile.backlog
import grails.converters.JSON
import grails.plugins.springsecurity.Secured
import com.ant.myagile.Project
import com.ant.myagile.Member
import com.ant.myagile.Team
import com.ant.myagile.Team_Project;

class RetrospectiveResultController {
	def springSecurityService
	
	@Secured(['ROLE_USER', 'ROLE_ADMIN'])
	static allowedMethods = [save: "POST", update: "POST", delete: "POST",index: "GET"]
	def index() {
		redirect(action: "listRetrospective", params: params)
	}
	
	def listRetrospective(){
		Member member = springSecurityService.currentUser
		def projects=Project.findAllByOwner(member)
		[projectList:projects]
	}
		
	def listSprintOfTeam(){
		Team team = Team.get(params.id)
		def lastSprintDate = Sprint.executeQuery("select id from Sprint where team="+params.id+" and dateEnd=(select max(dateEnd) from Sprint where team="+params.id+")")
		Sprint[] sprints = team.sprints
		render([sprintList:sprints,maxSprint:lastSprintDate] as JSON)
	}

	def showRetro(){
		Long proId = Long.parseLong(params.proId)
		Long teamId = Long.parseLong(params.teamId)
		def retroList = RetrospectiveResult.executeQuery("select a.description, a.type, a.status, a.id, a.date, a.owner "+
			"from RetrospectiveResult a, Sprint b "+
			"where a.project="+proId+
			" and b.team="+teamId+" and a.sprint = b.id order by a.date desc")
		render retroList as JSON
	}
	
	def newRetro(){
		[retroList:RetrospectiveResult.list()]
	}
    
	def addRetro(){
		def tId = params.tId
		def retro = new RetrospectiveResult()
		def sprint = Sprint.executeQuery("select id from Sprint where team="+tId+" order by dateEnd desc")
		println sprint
		retro.sprint = Sprint.get(sprint[0])
		retro.type = "Unhappy"
		retro.owner = springSecurityService.currentUser
		retro.description = params.description
		retro.status = "Unsolved"
		retro.project = Project.get(params.pId)
		retro.date = new Date()
		retro.save(failOnError:true)
		render([retro : retro] as JSON)
	}

	def updateType(){
		def retro  = RetrospectiveResult.get(params.id)
		retro.status = "Solved"
		retro.save(failOnError:true)
		render ([retro : retro] as JSON)
	}

	def getRetro(){
		def retro = RetrospectiveResult.get(params.id)
		render ([retro : retro] as JSON)
	}

	def updateRetro(){
		def retro = RetrospectiveResult.get(params.id)
		retro.description = params.de
		retro.save(failOnError:true)
		render([retro : retro] as JSON)
	}
	
	def delRetro(){
		def retro = RetrospectiveResult.get(params.id)
		retro.delete(failOnError: true)		
		return null
	}

	def editRetro(){
		def retro  = RetrospectiveResult.get(request.getParameter("id"))
	}


	def saveRetro(){
		RetrospectiveResult retro= new RetrospectiveResult()
		Sprint sprint = Sprint.get(params.sprintID)
		retro.type = params.type
		retro.description = params.description
		if(params.status==null){
			retro.status="Solved"
		}else{
			retro.status=params.status
		}		
		retro.sprint = sprint
		retro.owner = springSecurityService.currentUser
		retro.project = Project.get(params.pId)
		retro.date = new Date()
		if(retro.save()){
			render ([result:retro] as JSON)
		}
	}

	def showRetrospective(){	
		if(params.sprint){
			def result = RetrospectiveResult.findAllBySprint(Sprint.get(params.sprint))
			Sprint sprint =Sprint.get(params.sprint)
			def sprintStatus =sprint.status
			params.result = result
			params.sprintStatus = sprintStatus
			render params as JSON
		}
	}

	def saveEditRetro(){
		RetrospectiveResult retro =  RetrospectiveResult.get(params.id)
		Sprint sprinto = Sprint.get(params.sprintID)
		retro.sprint = sprinto
		retro.properties = params
		redirect(action: "listRetrospective", params: params)
	}

	def deleteRetro(long id){
		RetrospectiveResult Retro = RetrospectiveResult.get(id)
		Retro.delete()
	}
	
}

